(function() {
    'use strict';
    angular
        .module('byibserverApp')
        .factory('Product', Product)
        .factory('uploadImg',uploadImg)

    Product.$inject = ['$resource'];
    uploadImg.$inject = ['$resource'];

    function Product ($resource) {
        var resourceUrl =  'api/products/:id';

        return $resource(resourceUrl, {}, {
            'query': { method: 'GET', isArray: true},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    if (data) {
                        data = angular.fromJson(data);
                    }
                    return data;
                }
            },
            'update': { method:'PUT' }
        });
    }

    // 上传图片
    function  uploadImg($resource) {


       return function (url,baseImg,success,error) {
            //console.log(baseImg)
           var resource = $resource(url, {});

           resource.save({},{file:baseImg},function (data) {

                console.log("返回的数据",data)
                typeof  success=="function" && success(data)

           },function () {
               typeof  error=="function" &&error()
           })
       }
    }

})();
